package com.boot.proofing.controller.base;/**
 * @description
 * @autor xbwu on 2018/12/3.
 */

import com.boot.proofing.common.config.QuartzSchedulerCustomer;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务控制器
 * @author xbwu
 * @create 2018-12-03 
 **/
@RestController
@RequestMapping("/quartz")
public class QuartzApiController {

    @Autowired
    private QuartzSchedulerCustomer quartzScheduler;

    /**
     * 启动定时任务
     */
    @GetMapping("/start")
    public void startQuartzJob() {
        try {
            quartzScheduler.startJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查看定时任务
     */
    @GetMapping("/info")
    public String getQuartzJob(String name, String group) {
        String info = null;
        try {
            info = quartzScheduler.getJobInfo(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }
    /**
     * 修改定时任务的时间
     */
    @GetMapping("/modify")
    public boolean modifyQuartzJob(String name, String group, String time) {
        boolean flag = true;
        try {
            flag = quartzScheduler.modifyJob(name, group, time);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }
    /**
     * 暂停定时任务
     */
    @GetMapping(value = "/pause")
    public void pauseQuartzJob(String name, String group) {
        try {
            quartzScheduler.pauseJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 重新启动被暂停的定时任务
     */
    @GetMapping(value = "/resume")
    public void resumeQuartzJob(String name, String group) {
        try {
            quartzScheduler.resumeJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重新启动所有被暂停的定时任务
     */
    @GetMapping(value = "/resumeAll")
    public void resumeAllQuartzJob() {
        try {
            quartzScheduler.resumeAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 暂停所有定时任务
     */
    @GetMapping(value = "/pauseAll")
    public void pauseAllQuartzJob() {
        try {
            quartzScheduler.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }



    @GetMapping(value = "/delete")
    public void deleteJob(String name, String group) {
        try {
            quartzScheduler.deleteJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
